package edu.upol.paradise.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import edu.upol.paradise.hypervisor.ParadiseHypervisor;

public class HypervisorUtils {

	public static void readFully(InputStream inputStream, byte[] b, int off,
			int len) throws IOException {
		int offset = off;
		int readAmount = 0;
		while (readAmount != len){
			int ret = inputStream.read(b, offset, len-readAmount);
			if (ret == -1)
				throw new IOException("Socket read failure");
			readAmount += ret;
		}
	}

	public static void writeFully(OutputStream outputStream, byte[] b, int off,
			int len) throws IOException {
		outputStream.write(b, off, len);
		outputStream.flush();
	}

	public static boolean isLocalHost(String addr) {
		if (addr == null) return false;
		
		boolean ret = false;
		ret = ret || addr.equals("localhost");
		ret = ret || addr.equals("127.0.0.1");
		return ret;
		
	}
	
	public static boolean isCurrentHypervisor(String addr, int port){
		boolean ret = isLocalHost(addr);
		if (ret){
			if (!(port == ParadiseHypervisor.hypervisor.getSettings().getServerAddress()))
				ret = false;
		}
		return ret;
	}
	
}
